The RCC clock configuration varies significantly between STM32 series and is not generic. The register mapping, the set of available PLLs and prescalers, as well as the supported oscillators (HSE, HSI, CSI, LSE, LSI, MSI, etc.) can differ from one family to another. All screenshots and code snippets in this page are based on the STM32C5xx series and are intended only as illustrative examples. When applying the same concepts to other STM32 families, users must carefully review the RCC features and clock tree of the target device (e.g. number and type of PLLs, presence or absence of specific oscillators, dedicated dividers and muxes) and adapt the configuration accordingly.

HAL RCC Initialization and de-initialization functions

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 RCC configuration panel.

  • Configure clock sources and bus prescalers.

  • Configure the RCC main features.

  • Configure the RCC advanced features.

  • Configure the RCC GPIO pins.

  • Configure the EXTI line.

  • Configure the NVIC if the application requires the RCC to be used in IT mode.

  • Generate the code.

HAL RCC clock configuration

  • The RCC system clock source.

  • The AHB and APBx prescaler.

  • The cortex clock source and divider.

  • The peripherals kernel clock sources.

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

All these configurations can be performed either in the graphical view (as shown in the previous picture) or using the table view. The table view can make it easier to find the relevant configuration thanks to the “Search by Feature id” capability.

Project configuration panel

HAL RCC main features configuration

The RCC Main features block configure:

  • The Flash latency (wait states).

  • The Flash programming delay.

  • The HSE source, its startup timeout and the Clock Security System.

  • The LSE source, its startup timeout, drive capability and the Clock Security System.

RCC main feature configuration

HAL RCC advanced features configuration

Configure the advanced RCC features.

RCC advanced feature configuration

GPIO configuration

Configure GPIO pins only if the MCU uses an external oscillator (HSE/LSE) and/or you need to output a clock on MCO or LSCO.

RCC GPIO configuration

EXTI configuration

Configure the EXTI line for LSECSS.

RCC EXTI configuration

NVIC configuration

Configure the NVIC in case of an interrupt.

RCC NVIC configuration

Code snippets of default HAL2 clock configuration

system_status_t mx_rcc_init(void)
{
  if (HAL_RCC_HSIDIV3_Enable() != HAL_OK)
  {
    return SYSTEM_CLOCK_ERROR;
  }

  /** Initializes the CPU, AHB and APB bus clocks */
  hal_rcc_bus_clk_config_t config_bus;
  config_bus.hclk_prescaler  = HAL_RCC_HCLK_PRESCALER1;
  config_bus.pclk1_prescaler = HAL_RCC_PCLK_PRESCALER1;
  config_bus.pclk2_prescaler = HAL_RCC_PCLK_PRESCALER1;
  config_bus.pclk3_prescaler = HAL_RCC_PCLK_PRESCALER1;
  if (HAL_RCC_SetBusClockConfig(&config_bus) != HAL_OK)
  {
    return SYSTEM_CLOCK_ERROR;
  }

  if (HAL_RCC_SetSYSCLKSource(HAL_RCC_SYSCLK_SRC_HSIDIV3) != HAL_OK)
  {
    return SYSTEM_CLOCK_ERROR;
  }

  /** Frequency will be decreased */
  HAL_FLASH_ITF_SetLatency(HAL_FLASH, HAL_FLASH_ITF_LATENCY_1);
  HAL_FLASH_ITF_SetProgrammingDelay(HAL_FLASH, HAL_FLASH_ITF_PROGRAM_DELAY_);
  /** Systick Initialization */
  HAL_RCC_SetSysTickExternalClkSource(HAL_RCC_SYSTICK_CLK_SRC_HCLKDIV8);

  if (HAL_UpdateCoreClock() != HAL_OK)
  {
    return SYSTEM_CLOCK_ERROR;
  }

  return SYSTEM_OK;
}

Note

The RCC clock configuration is generated in the mx_rcc_hal_init function in the mx_rcc.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.


Oscillator Configuration

HAL1

HAL2

Function Name

HAL_RCC_OscConfig

Multiple functions for specific configurations

Purpose

Initialize the RCC Oscillators according to the specified parameters in the RCC_OscInitTypeDef .

Provide unitary functions for enabling, disabling, and configuring specific oscillators.

Implementation

The function HAL_RCC_OscConfig performs the following steps:

  • Initialize the RCC Oscillators based on the configuration provided in RCC_OscInitTypeDef.

  • Manage PLL configurations.

The functionality in HAL2 is divided into multiple unitary functions:

hal_status_t        HAL_RCC_HSI_EnableInStopMode(void);
hal_status_t        HAL_RCC_HSI_DisableInStopMode(void);
hal_status_t        HAL_RCC_HSIS_Enable(void);
hal_status_t        HAL_RCC_HSIS_Disable(void);

hal_status_t        HAL_RCC_HSIDIV4_Enable(void);
hal_status_t        HAL_RCC_HSIDIV4_Disable(void);

hal_status_t        HAL_RCC_HSIDIV3_Enable(void);
hal_status_t        HAL_RCC_HSIDIV3_Disable(void);

hal_status_t        HAL_RCC_HSIK_Enable(hal_rcc_hsik_div_t divider);
hal_status_t        HAL_RCC_HSIK_Disable(void);

/* LSI oscillator configuration and activation    ******************************/
hal_status_t        HAL_RCC_LSI_Enable(void);
hal_status_t        HAL_RCC_LSI_Disable(void);

/* HSE oscillator configuration and activation    ******************************/
hal_status_t        HAL_RCC_HSE_Enable(hal_rcc_hse_t mode);
hal_status_t        HAL_RCC_HSE_Disable(void);

/* LSE oscillator configuration and activation    ******************************/
hal_status_t        HAL_RCC_LSE_Enable(hal_rcc_lse_t mode, hal_rcc_lse_drive_t drive);
hal_status_t        HAL_RCC_LSE_Disable(void);

/* PSI oscillator configuration and activation    ******************************/
hal_status_t        HAL_RCC_PSI_EnableInStopMode(void);
hal_status_t        HAL_RCC_PSI_DisableInStopMode(void);
hal_status_t        HAL_RCC_PSIS_Enable(void);
hal_status_t        HAL_RCC_PSIS_Disable(void);

hal_status_t        HAL_RCC_PSIDIV4_Enable(void);
hal_status_t        HAL_RCC_PSIDIV4_Disable(void);

hal_status_t        HAL_RCC_PSIDIV3_Enable(void);
hal_status_t        HAL_RCC_PSIDIV3_Disable(void);

hal_status_t        HAL_RCC_PSIK_Enable(hal_rcc_psik_div_t divider);
hal_status_t        HAL_RCC_PSIK_Disable(void);

Related use cases: System Clock Configuration

System Clock Configuration

HAL1

HAL2

Function Name

HAL_RCC_ClockConfig

Multiple functions for specific configurations

Purpose

Configure the system clock according to the specified parameters in the RCC_ClkInitTypeDef .

Provide unitary functions for setting and retrieving the system clock source and prescalers.

Implementation

The function HAL_RCC_ClockConfig performs the following steps:

  • Configure the system clock based on the configuration provided in RCC_ClkInitTypeDef.

  • Manage the clock source and prescalers.

The functionality in HAL2 is divided into multiple unitary functions:

hal_status_t  HAL_RCC_SetSYSCLKSource(hal_rcc_sysclk_src_t source);
hal_status_t  HAL_RCC_PSI_SetConfig(const hal_rcc_psi_config_t *p_config);
hal_status_t  HAL_RCC_SetHCLKPrescaler(hal_rcc_hclk_prescaler_t prescaler);
void          HAL_RCC_SetPCLK1Prescaler(hal_rcc_pclk_prescaler_t prescaler);
void          HAL_RCC_SetPCLK2Prescaler(hal_rcc_pclk_prescaler_t prescaler);
void          HAL_RCC_SetPCLK3Prescaler(hal_rcc_pclk_prescaler_t prescaler);
hal_status_t  HAL_RCC_SetBusClockConfig(const hal_rcc_bus_clk_config_t *p_bus_clk_dividers);

System Clock Reset

HAL1

HAL2

Function Name

HAL_RCC_DeInit

HAL_RCC_ResetSystemClock HAL_RCC_Reset

Purpose

De-initialize the RCC system clock configuration and reset the RCC registers to their default values.

HAL_RCC_ResetSystemClock: Reset the system clock configuration to a known state using the default clock source after reset.

HAL_RCC_Reset: Reset all RCC registers to their default values.

Implementation

The function HAL_RCC_DeInit performs a comprehensive reset of the RCC configuration, including:

  • Increasing CPU frequency if necessary.

  • Enabling the oscillator used by default and setting it as the system clock source.

  • Resetting the CFGR register.

  • Disabling various oscillators and resetting PLL configurations.

  • Disabling all RCC interrupts and clearing all interrupt flags.

  • Updating the SystemCoreClock variable.

  • Adjusting the Flash latency based on the new CPU frequency.

  • Adapting the SysTick interrupt period.

The function HAL_RCC_ResetSystemClock focuses on:

  • Enabling the default oscillator and selecting it as the system clock source.

  • Updating the SystemCoreClock variable.

  • Adapting the SysTick interrupt period.

The function HAL_RCC_Reset provides an extended/global reset of the RCC configuration, including:

  • Resetting the RCC clock configuration to the default reset state.

  • Updating SystemCoreClock and the HAL timebase.

  • Disabling oscillators and resetting PLL configurations as part of the RCC reset sequence.

  • Disabling and clearing RCC-related interrupts as applicable.

  • Resetting resources in the backup domain if USE_HAL_RCC_RESET_RTC_DOMAIN is set to 1U.

  • Resetting peripheral clock enables and clock source selections if USE_HAL_RCC_RESET_PERIPH_CLOCK_MANAGEMENT is set to 1U.

Rationale: In HAL2, HAL_RCC_ResetSystemClock offers a focused way to restore the system clock to a valid state after reset (default oscillator, system clock selection, SystemCoreClock update and SysTick adjustment). HAL_RCC_Reset extends this by providing a global RCC reset that also covers oscillator disabling, PLL reset and interrupt handling, as well as backup domain and peripheral clock management when the corresponding options are enabled.

Oscillator Configuration Retrieval

HAL1

HAL2

Function Name

HAL_RCC_GetOscConfig

Multiple functions for specific oscillator status retrieval

Purpose

Retrieve the current configuration of the RCC oscillators.

Provide unitary functions for retrieving the status of specific oscillators.

Implementation

The function HAL_RCC_GetOscConfig retrieves the current configuration of the RCC oscillators and populates the RCC_OscInitTypeDef structure.

The functionality in HAL2 is divided into multiple unitary functions for retrieving the status of specific oscillators and PLL:

hal_rcc_osc_stop_mode_status_t HAL_RCC_HSI_IsEnabledInStopMode(void);
hal_rcc_osc_enable_status_t    HAL_RCC_HSIS_IsEnabled(void);
hal_rcc_osc_ready_status_t     HAL_RCC_HSIS_IsReady(void);

hal_rcc_osc_enable_status_t    HAL_RCC_HSIDIV4_IsEnabled(void);
hal_rcc_osc_ready_status_t     HAL_RCC_HSIDIV4_IsReady(void);

hal_rcc_osc_enable_status_t    HAL_RCC_HSIDIV3_IsEnabled(void);
hal_rcc_osc_ready_status_t     HAL_RCC_HSIDIV3_IsReady(void);

hal_rcc_osc_enable_status_t    HAL_RCC_HSIK_IsEnabled(void);
hal_rcc_osc_ready_status_t     HAL_RCC_HSIK_IsReady(void);

hal_rcc_osc_enable_status_t    HAL_RCC_LSI_IsEnabled(void);
hal_rcc_osc_ready_status_t     HAL_RCC_LSI_IsReady(void);

hal_rcc_osc_enable_status_t    HAL_RCC_HSE_IsEnabled(void);
hal_rcc_osc_ready_status_t     HAL_RCC_HSE_IsReady(void);

hal_rcc_osc_enable_status_t    HAL_RCC_LSE_IsEnabled(void);
hal_rcc_osc_ready_status_t     HAL_RCC_LSE_IsReady(void);

hal_rcc_osc_stop_mode_status_t HAL_RCC_PSI_IsEnabledInStopMode(void);
hal_rcc_osc_enable_status_t    HAL_RCC_PSIS_IsEnabled(void);
hal_rcc_osc_ready_status_t     HAL_RCC_PSIS_IsReady(void);

hal_rcc_osc_enable_status_t    HAL_RCC_PSIDIV4_IsEnabled(void);
hal_rcc_osc_ready_status_t     HAL_RCC_PSIDIV4_IsReady(void);

hal_rcc_osc_enable_status_t    HAL_RCC_PSIDIV3_IsEnabled(void);
hal_rcc_osc_ready_status_t     HAL_RCC_PSIDIV3_IsReady(void);

hal_rcc_osc_enable_status_t    HAL_RCC_PSIK_IsEnabled(void);
hal_rcc_osc_ready_status_t     HAL_RCC_PSIK_IsReady(void);

System Clock Configuration Retrieval

HAL1

HAL2

Function Name

HAL_RCC_GetClockConfig

Multiple functions for specific clock configuration retrieval

Purpose

Retrieve the current configuration of the system clock.

Provide unitary functions for retrieving the prescalers and clock source.

Implementation

The function HAL_RCC_GetClockConfig retrieves the current configuration of the system clock and populates the RCC_ClkInitTypeDef structure.

The functionality in HAL2 is divided into multiple unitary functions for retrieving the prescalers and clock source:

hal_rcc_hclk_prescaler_t HAL_RCC_GetHCLKPrescaler(void);
hal_rcc_pclk_prescaler_t HAL_RCC_GetPCLK1Prescaler(void);
hal_rcc_pclk_prescaler_t HAL_RCC_GetPCLK2Prescaler(void);
hal_rcc_pclk_prescaler_t HAL_RCC_GetPCLK3Prescaler(void);
hal_rcc_sysclk_src_t     HAL_RCC_GetSYSCLKSource(void);